dofile (getScriptPath() .. "\\..\\processor\\processor.lua")
dofile (getScriptPath() .. "\\..\\processor\\log.lua")

dofile (getScriptPath() .. "\\..\\lib\\lib.lua")

--dofile (getScriptPath() .. "\\..\\patterns\\gap.lua")
--dofile (getScriptPath() .. "\\..\\patterns\\headoff.lua")
--dofile (getScriptPath() .. "\\..\\patterns\\move.lua")
--dofile (getScriptPath() .. "\\..\\patterns\\freeze.lua")
--dofile (getScriptPath() .. "\\..\\patterns\\imp.lua")
--dofile (getScriptPath() .. "\\..\\patterns\\jet.lua")
dofile (getScriptPath() .. "\\..\\patterns\\press.lua")

script_name = string.gsub(string.gsub(debug.getinfo(1).short_src, "%.[^%.]*$", ""), "^.*[\\/]", "")
--processor.on_run_delay = 30

Settings = {}

show = {}

processor:AddInit(function()
	client_code = lib:GetUser()
	if not client_code then return true end
	script_id = client_code .. "#" .. script_name
	log_file = lib.path.log .. script_id .. ".log"
end)

processor:AddInit(function()
	show.charts = lib:GetChartList("CHART", "VIEW")
	if #show.charts < 1 then
		return true
	end
end)

processor:AddInit(function()
	for i, v in ipairs(show.charts) do
		show.charts[v] = {index = i}
	end
end)

processor:AddInit(function()
	processor:InitLog(log_file)
	processor:log("started")
end)

processor:AddTimer(function()
	for i, v in ipairs(show.charts) do
		show:UpdatePatterns(v)
	end
end)

processor:AddStop(function()
	for i, v in ipairs(show.charts) do
		show:Remove(v)
		show:Remove(v, "t%.")
	end
end)

function show:UpdatePatterns(tag)
	if self.charts[tag] then
		local g = lib:GetChartGraph(tag)
		local c = lib:GetLastCandle(tag)
		
		if self.charts[tag].graph ~= g
			or c and self.charts[tag].last_time ~= lib:tickseconds(c.datetime)
			or self.charts[tag].last_label and not GetLabelParams(tag, self.charts[tag].last_label)
		then
			self.charts[tag].graph = g
			if c then
				self.charts[tag].last_time = lib:tickseconds(c.datetime)
			end
			self.charts[tag].instrument = lib:GetChartInstrument(tag)
			
			self:ShowPatterns(tag)
		end
	end
end

function show:ShowPatterns(tag)
	self:Remove(tag)
	self.charts[tag].last_label = nil
	
	local label_params = {
				TRANSPARENT_BACKGROUND = 1,
				FONT_HEIGHT = 8,
				R = 255,
				G = 255,
				B = 255
			}
	local data
	local price, datetime
	local l
	
	for k, v in pairs(pattern) do
		if v.OnCandle then
			data = {}
			label_params.TEXT = k
			for i = math.min(5000, getNumCandles(tag)), 1, -1 do
				price, datetime = v.OnCandle(tag, i, data)
				if price and datetime then
					label_params.HINT = datetime.hint__
					label_params.DATE = datetime.year * 10000 + datetime.month * 100 + datetime.day
					label_params.TIME = datetime.hour * 10000 + datetime.min * 100 + datetime.sec
					label_params.YVALUE = price
					l = lib:AddLabel(tag, label_params)
					if l then self.charts[tag].last_label = l end
				end
			end
		end
	end
	
	--self:ShowTicks(tag)
end

function show:ShowTicks(tag, date)
	if self.charts[tag].instrument then
		local d
		if date then
			d = lib:FormatDate(date)
			local file = lib.path.labels .. "ticks." .. d
			local hf = io.open(file, "r")
			if hf then
				local lines = hf:read("*a")
				hf:close()
				local label_params = {
							TRANSPARENT_BACKGROUND = 1,
							FONT_HEIGHT = 8,
							R = 255,
							G = 255,
							B = 0
						}
				local datetime, l
				local n = 0
				for line in string.gmatch(lines, "([^\n]+)\n") do
					datetime = lib:LoadString(line)
					if datetime
						and datetime.sec_code__ == self.charts[tag].instrument.code
					then
						n = n + 1
						label_params.TEXT = "t." .. string.lower(string.match(datetime.hint__, "^%S+"))
						label_params.HINT = datetime.hint__
						label_params.DATE = datetime.year * 10000 + datetime.month * 100 + datetime.day
						label_params.TIME = datetime.hour * 10000 + datetime.min * 100 + datetime.sec
						label_params.YVALUE = datetime.price__
						l = lib:AddLabel(tag, label_params)
						if l then self.charts[tag].last_label = l end
					end
				end
			end
		else
			self:Remove(tag, "t%.")
			local c
			for i = 0, math.min(999, getNumCandles(tag) - 1) do
				c = lib:GetLastCandle(tag, i)
				if c then
					if d ~= lib:FormatDate(c.datetime) then
						d = lib:FormatDate(c.datetime)
						self:ShowTicks(tag, c.datetime)
					end
				end
			end
		end
	end
end

function show:Remove(tag, text)
	if tag then
		if text then
			local lp
			for i = lib:GetMaxLabelID(tag) * 2, 0, -1 do
				lp = GetLabelParams(tag, i)
				if lp and lp.yvalue
					and string.match(lp.text, "^" .. text)
				then
					DelLabel(tag, i)
				end
			end
		else
			for k, v in pairs(pattern) do
				self:Remove(tag, k)
			end
		end
	end
end


